<template>
  <div>
        <el-form :model="queryParams" ref="queryForm" :inline="true"  label-width="68px"  @submit.native.prevent>
          <el-form-item label="菜单名称" prop="name">
            <el-input
              v-model="queryParams.name"
              placeholder="精准匹配"
              clearable
              size="small"
              style="width: 240px"
              @keyup.enter.native="selectMenu"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="selectMenu">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
             @click="topAdd"
            >新增</el-button>
          </el-col>
        </el-row>
      <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="menuList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      :header-cell-style="getRowClass"
    >
      <el-table-column prop="name" label="菜单名称" width="260"></el-table-column>
      <el-table-column prop="imgUrl" label="图标" align="center" width="100">
        <template slot-scope="scope">
          <i :class="scope.row.imgUrl" />
        </template>
      </el-table-column>
      <el-table-column prop="appUrl" label="组件路径" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="id" label="菜单编号" width="100"></el-table-column>
      <el-table-column prop="sortCode" label="排序" width="200"></el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="200">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
        

   <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级菜单" prop="parentName">
              <el-input v-model="form.parentName" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单图标" prop="imgUrl">
              <e-icon-picker v-model="form.imgUrl"/>
            </el-form-item>
           
          </el-col>
          <el-col :span="12">
            <el-form-item label="菜单名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入菜单名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="sortCode">
              <el-input-number v-model="form.sortCode" controls-position="right" :min="1" />
            </el-form-item>
          </el-col>
         
          <el-col :span="12">
            <el-form-item label="组件路径" prop="appUrl">
              <el-input v-model="form.appUrl" placeholder="请输入组件路径" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getStatusApi} from "@/api/system/common"
import {selectMenuApi,deleteMenuApi,updateMenuApi,addMenuApi} from "@/api/system/menu"
export default {
  name: "user",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      refreshTable: true,
       // 状态数据
      statusOptions: [],
      // 表格数据
      menuList: [],
      //  // 菜单树选项
      // menuOptions: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
       // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      form: {},
      // 查询参数
      queryParams: {
        name: undefined
      },
      // 列信息
      columns: [
       
      ],
      // 表单校验
      rules: {
        
      }
    };
  },
  watch: {
   
  },
  created() {
    this.selectMenu();
    getStatusApi().then(data=>{
      this.statusOptions = data.data;
    });
  },
  methods: {
    getRowClass ({ rowIndex }) {
      if (rowIndex == 0) {
        return 'background:#afdfe4'
      } else {
        return ''
      }
    },
    //查询
    selectMenu(){
      selectMenuApi(this.queryParams).then(data=>{
        this.menuList=  data.data;
        this.loading = false;
      }).catch(()=>{
        this.loading = false;
      })
    },
    /** 新增按钮操作 */
    topAdd() {
      this.reset();
      this.form.parentId = 0;
      this.form.parentName = "主类目";
      this.form.parentIds = "0";
      this.title = "添加菜单";
      this.open = true;
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.form.parentId = row.id;
      this.form.parentName = row.name;
      this.form.parentIds = row.parentIds;
      this.title = "添加菜单";
      this.open = true;
    },
    /** 重置按钮操作 */
    resetQuery() {
      if (this.$refs["queryForm"]) {
		    this.$refs["queryForm"].resetFields();
	    }
      this.selectMenu();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除菜单编号为"' + row.id + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(()=> {
          deleteMenuApi({
            "id": row.id
          }).then(data=>{
            this.selectMenu();
            this.$message({
               type: 'success',
              message: '删除成功'
            });
          })
        })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
        this.form.id = row.id;
        this.form.sortCode = row.sortCode;
        this.form.parentName = row.parentName;
        if(row.parentId=="0"){
          this.form.parentName = "主类目";
        }
        this.form.imgUrl = row.imgUrl;
        this.form.appUrl = row.appUrl;
        this.form.name = row.name;
        this.title = "修改菜单";
        this.open = true;  
    }, // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        parentId: undefined,
        appUrl: undefined,
        imgUrl: undefined,
        sortCode: undefined,
        parentName: undefined,
        status: undefined,
      };
       if (this.$refs["form"]) {
		    this.$refs["form"].resetFields();
	    }
    },
     /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateMenuApi(this.form).then(data=>{
              this.open = false;
              this.selectMenu();
              this.reset();
              this.$message({
                type: 'success',
                message: '修改成功'
              });
            })
          } else {
            addMenuApi(this.form).then(data=>{
              this.open = false;
              this.selectMenu();
              this.reset();
              this.$message({
                type: 'success',
                message: '添加成功'
              });
            })
          }
        }
      });
    }, // 取消按钮
    cancel() {
      this.open = false;
    }
  }
};
</script>